home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / filesyst / ncpfs / mars_dos.000 / mars_dos / netpc / login.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-22  |  9.9 KB  |  414 lines

  1. /* login.c 21-May-96 */
  2.  
  3. /****************************************************************
  4.  * (C)opyright (C) 1993,1996  Martin Stover, Marburg, Germany   *
  5.  ****************************************************************/
  6.  
  7. #include "net.h"
  8. #include "nwcrypt.h"
  9.  
  10. static int do_change_object_passwd(char *name,
  11.                                    uint16 objtyp,
  12.                                    char *oldpassword,
  13.                                    char *newpassword)
  14.  
  15. {
  16.   uint8 key[8];
  17.   if (!ncp_17_17(key)) {
  18.     /* now crypted change password */
  19.     uint32 objid = ncp_17_35(name, objtyp);
  20.     if (objid) {
  21.       uint8 buff_o[16];
  22.       uint8 buff_n[16];
  23.       uint8 encrypted[8];
  24.       uint8 newcryptpasswd[16];
  25.       int   len_oldpasswd=strlen(oldpassword);
  26.       int   len_newpasswd=strlen(newpassword);
  27.       uint8 tmpid[4];
  28.       U32_TO_BE32(objid, tmpid);
  29.  
  30.       shuffle(tmpid, oldpassword, len_oldpasswd, buff_o);
  31.       nw_encrypt(key, buff_o, encrypted);
  32.       shuffle(tmpid, newpassword, len_newpasswd, buff_n);
  33.  
  34.       newpassencrypt(buff_o,    buff_n,   newcryptpasswd);
  35.       newpassencrypt(buff_o +8, buff_n+8, newcryptpasswd+8);
  36.  
  37.       if (!oldpassword[0]) oldpassword[1] = '\0';
  38.  
  39.       if (!ncp_17_4b(encrypted, name, objtyp,
  40.          (len_newpasswd ^ oldpassword[0] ^ oldpassword[1]) & 0x7f | 0x40,
  41.            newcryptpasswd) ) {
  42.         ;;
  43.         return(0);
  44.       }
  45.  
  46.     }
  47.   } else { /* now we use old unencrypted algorithmus */
  48.     if (!ncp_17_40(name, objtyp, oldpassword, newpassword)) {
  49.       ;;
  50.       return(0);
  51.     }
  52.   }
  53.   return(-1);
  54. }
  55.  
  56. static int do_object_login(char *name, uint16 objtyp, char *password, int option)
  57. {
  58.   uint8 key[8];
  59.   if (!(option & 1) && !ncp_17_17(key)) {
  60.     uint32 objid = ncp_17_35(name, objtyp);
  61.     if (objid) {
  62.       uint8 buff[128];
  63.       uint8 encrypted[8];
  64.       uint8 tmpid[4];
  65.       U32_TO_BE32(objid, tmpid);
  66.       shuffle(tmpid, password, strlen(password), buff);
  67.       nw_encrypt(key, buff, encrypted);
  68.       if (!ncp_17_18(encrypted, name, objtyp)) {
  69.         ;;
  70.         return(0);
  71.       }
  72.     }
  73.   } else { /* now we use old unencrypted algorithmus */
  74.     if (!ncp_17_14(name, objtyp, password)) {
  75.       return(0);
  76.     }
  77.   }
  78.   return(-1);
  79. }
  80.  
  81. static void beep(void)
  82. {
  83.   fprintf(stdout, "\007");
  84. }
  85.  
  86. static int get_raw_str(uint8 *s, int maxlen, int doecho)
  87. /* returns len of readed str */
  88. {
  89.   int len = 0;
  90.   while (len < maxlen){
  91.     int key = getch();
  92.     if (key == '\r' || key == '\n') break;
  93.     switch (key) {
  94.       case    8 : if (len) {
  95.                     --len;
  96.                     --s;
  97.                     if (doecho) fprintf(stdout, "\010 \010");
  98.                   } else beep();
  99.                   continue;
  100.  
  101.       case  '\t': beep();
  102.                   continue;
  103.  
  104.       default   : *s++=(uint8)key;
  105.                   len++;
  106.                 break;
  107.     } /* switch */
  108.     if (doecho) fprintf(stdout, "%c", (uint8)key);
  109.   }
  110.   *s='\0';
  111.   return(len);
  112. }
  113.  
  114. static void getstr(char *what, char *str, int rsize, int doecho)
  115. {
  116.   fprintf(stdout, "%s: ", what);
  117.   get_raw_str(str, rsize, doecho);
  118.   fprintf(stdout, "\n");
  119. }
  120.  
  121. static int login_usage(void)
  122. {
  123.   fprintf(stderr, "usage:\t%s [-u] [user | user password]\n", funcname);
  124.   fprintf(stderr, "\t-u : use unecrypted password\n" );
  125.   return(-1);
  126. }
  127.  
  128. int func_login(int argc, char *argv[], int mode)
  129. {
  130.   int result=-1;
  131.   int option=0;
  132.   uint8 uname[200];
  133.   uint8 upasswd[200];
  134.   SEARCH_VECTOR  save_drives;
  135.  
  136.   if (argc > 1) {
  137.     if (argv[1][0] == '-') {
  138.       if (argv[1][1] == 'u') option |= 1;
  139.       else return(login_usage());
  140.       argc--;
  141.       argv++;
  142.     }
  143.   }
  144.   get_search_drive_vektor(save_drives);
  145.   remove_nwpathes();
  146.   if (argc > 1) strmaxcpy(uname, argv[1], sizeof(uname) -1);
  147.   else uname[0]='\0';
  148.   if (argc > 2) strmaxcpy(upasswd, argv[2], sizeof(upasswd) -1);
  149.   else upasswd[0]='\0';
  150.  
  151.   while (result) {
  152.     if (!uname[0]) getstr("Login", uname, sizeof(uname)-1, 1);
  153.     if (uname[0]) {
  154.       upstr(uname);
  155.       upstr(upasswd);
  156.       if ((result = do_object_login(uname, 0x1,  upasswd, option)) < 0 && !*upasswd) {
  157.         getstr("Password", upasswd, sizeof(upasswd)-1, 0);
  158.         upstr(upasswd);
  159.         result = do_object_login(uname, 0x1, upasswd, option);
  160.       }
  161.       if (result < 0) {
  162.         fprintf(stdout, "Login incorrect\n\n");
  163.         uname[0]   = '\0';
  164.         upasswd[0] = '\0';
  165.       }
  166.     } else break;
  167.   }
  168.   if (result > -1) {
  169.     char profile[200];
  170.     remove_nwpathes();
  171.     sprintf(profile, "%slogin", prgpath);
  172.     read_command_file(profile);
  173.   } else {
  174.     (void)set_search_drive_vektor(save_drives);
  175.   }
  176.   return(result);
  177. }
  178.  
  179. int func_logout(int argc, char *argv[], int mode)
  180. {
  181.   remove_nwpathes();
  182.   if (logout()) {
  183.     fprintf(stderr, "logout=%d\n", neterrno);
  184.     return(1);
  185.   }
  186.   return(0);
  187. }
  188.  
  189.  
  190. int func_passwd(int argc, char *argv[], int mode)
  191. {
  192.   int result=0;
  193.   uint8 uname[100];
  194.   uint8 upasswd[130];
  195.   uint32 my_obj_id;
  196.  
  197.   if (ncp_14_46(&my_obj_id) < 0 || my_obj_id == MAX_U32 || !my_obj_id) {
  198.     fprintf(stderr, "Cannot get actual user id\n");
  199.     result = -1;
  200.   }
  201.  
  202.   if (!result && argc > 1) {
  203.     uint32 obj_id;
  204.     strmaxcpy(uname, argv[1], sizeof(uname) -1);
  205.     upstr(uname);
  206.     obj_id = ncp_17_35(uname,  1);
  207.     if (!obj_id) {
  208.       fprintf(stderr, "Unkwown user: %s\n", uname);
  209.       return(-1);
  210.     }
  211.   } else if (!result) {
  212.     uint16 obj_typ;
  213.     if (ncp_17_36(my_obj_id, uname, &obj_typ) || obj_typ != 1) {
  214.       fprintf(stderr, "Cannot get actual username\n");
  215.       result=-1;
  216.     }
  217.   }
  218.   if (!result && *uname) {
  219.     uint8 newpasswd[130];
  220.     uint8 newpasswd2[130];
  221.     if (my_obj_id == 1L) *upasswd='\0';
  222.     else {
  223.       getstr("Old password", upasswd, sizeof(upasswd)-1, 0);
  224.       upstr(upasswd);
  225.     }
  226.     getstr("New password", newpasswd, sizeof(newpasswd)-1, 0);
  227.     getstr("New password again", newpasswd2, sizeof(newpasswd2)-1, 0);
  228.     if (!strcmp(newpasswd, newpasswd2)) {
  229.       upstr(uname);
  230.       upstr(newpasswd);
  231.       if (do_change_object_passwd(uname, 1, upasswd, newpasswd) < 0)
  232.          result = -1;
  233.     } else {
  234.       result = -1;
  235.       fprintf(stderr, "Password misspelled\n");
  236.     }
  237.   }
  238.   if (result < 0) fprintf(stderr, "Password not changed");
  239.   return(result);
  240. }
  241.  
  242. static int get_line(FILE *f, char *buff, int bufsize, uint8 *str, int strsize)
  243. /* returns command line or -1 if ends */
  244. {
  245.   if ((FILE*) NULL != f) {
  246.     while (fgets(buff, bufsize, f) != NULL){
  247.       char *p       = buff;
  248.       char *beg     = NULL;
  249.       char c;
  250.       int  len=0;
  251.       while (0 != (c = *p++) && c != '\n' && c != '\r' && c != '#') {
  252.         if (!beg){
  253.           if (c != '\t' && c != 32) {
  254.             beg = p - 1;
  255.             len = 1;
  256.           }
  257.         } else ++len;
  258.       }
  259.       if (len) {
  260.         strmaxcpy((uint8*)str, (uint8*)beg, min(len, strsize-1));
  261.         return(0);
  262.       }
  263.     }
  264.   }
  265.   return(-1);
  266. }
  267.  
  268.  
  269.  
  270. static char **build_argv(char *buf, int bufsize, char *command)
  271. /* routine returns **argv for use with execv routines */
  272. /* buf will contain the path component                    */
  273. {
  274.   int len        = strlen(command);
  275.   int offset     = ((len+4) / 4) * 4; /* aligned offset for **argv */
  276.   int components = (bufsize - offset) / 4;
  277.   if (components > 1) {  /* minimal argv[0] + NULL */
  278.     char **argv  = (char **)(buf+offset);
  279.     char **pp    = argv;
  280.     char  *p     = buf;
  281.     char  c;
  282.     int   i=0;
  283.     --components;
  284.     memcpy(buf, command, len);
  285.     memset(buf+len, 0, bufsize - len);
  286.     *pp    = p;
  287.     while ((0 != (c = *p++)) && i < components) {
  288.       if (c == 32 || c == '\t') {
  289.         *(p-1) = '\0';
  290.         if (*p != 32 && *p != '\t') {
  291.           *(++pp)=p;
  292.           i++;
  293.         }
  294.       } else if (!i && c == '/') {  /* here i must get argv[0] */
  295.         *pp=p;
  296.       }
  297.     }
  298.     return(argv);
  299.   }
  300.   return(NULL);
  301. }
  302.  
  303. int read_command_file(char *fstr)
  304. {
  305.   FILE *f=fopen(fstr, "r");
  306.   int  result=-1;
  307.   if (f != NULL) {
  308.     char *linebuf= xmalloc(512);
  309.     char *buf    = xmalloc(512);
  310.  
  311.     while (get_line(f, buf, 512, linebuf, 512) > -1) {
  312.       char **argv=build_argv(buf, 512, linebuf);
  313.       if (argv != NULL) {
  314.         int argc=0;
  315.         char **pp=argv;
  316.         while (*pp) {
  317.           argc++;
  318.           pp++;
  319.         }
  320.         upstr(argv[0]);
  321.         if (argc > 2 && !strcmp(argv[0], "ECHO")) {
  322.           char *p=argv[argc-1];
  323.           while (p-- > argv[1]) {
  324.             if (*p=='\0') *p=32;
  325.           }
  326.           argc=2;
  327.         }
  328.         call_func_entry(argc, argv);
  329.         result = 0;
  330.       }
  331.     }
  332.  
  333.     fclose(f);
  334.     xfree(linebuf);
  335.     xfree(buf);
  336.   } else result=-2;
  337.   return(result);
  338. }
  339.  
  340. int func_profile(int argc, char *argv[], int mode)
  341. {
  342.   if (argc < 2) {
  343.     fprintf(stderr, "usage:\t%s fn\n", funcname);
  344.     return(-1);
  345.   }
  346.   if (read_command_file(argv[1]) == -2) {
  347.     fprintf(stderr, "command file %s not found\n", argv[1]);
  348.   }
  349.   return(0);
  350. }
  351.  
  352. int func_cwd(int argc, char *argv[], int mode)
  353. {
  354.   char pathname[65];
  355.   int  len;
  356.   if (argc < 2) {
  357.     fprintf(stderr, "usage:\t%s path\n", funcname);
  358.     return(-1);
  359.   }
  360.   strmaxcpy(pathname, argv[1], sizeof(pathname) -1);
  361.   korrpath(pathname);
  362.   if (0 != (len = strlen(pathname))) {
  363.     char *p=pathname+len-1;
  364.     if (*p == '/' || *p == ':') {
  365.       *(++p) = '.';
  366.       *(++p) = '\0';
  367.       len++;
  368.     }
  369.     if (!chdir(pathname)) {
  370.       if (len > 2 && *(pathname+1) == ':')   /* device changed */
  371.         setdisk(*pathname - 'a' );
  372.     } else {
  373.       fprintf(stderr, "cannot chdir to %s\n", pathname);
  374.       return(1);
  375.     }
  376.     return(0);
  377.   } else return(-1);
  378. }
  379.  
  380. int func_echo(int argc, char *argv[], int mode)
  381. {
  382.   if (argc > 1)
  383.     fprintf(stdout, "%s\n", argv[1]);
  384.   return(0);
  385. }
  386.  
  387. int func_exec(int argc, char *argv[], int mode)
  388. {
  389.   if (argc > 1)  {
  390.     char *buf   = xmalloc(512);
  391.     char *buff  = xmalloc(512);
  392.     char *p     = buff;
  393.     int   k     = 0;
  394.     char **nargv;
  395.     while (++k < argc) {
  396.       strcpy(p, argv[k]);
  397.       p    += strlen(argv[k]);
  398.       *p++  = 32;
  399.       *p    = '\0';
  400.     }
  401.     nargv=build_argv(buf, 512, buff);
  402.     xfree(buff);
  403.     if (nargv != NULL) {
  404.       if (!mode)
  405.         spawnvp(P_WAIT, buf, nargv);
  406.       else
  407.         execvp(buf, nargv);
  408.     }
  409.     xfree(buf);
  410.   }
  411.   return(0);
  412. }
  413.  
  414.